package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yf1706.bean.EmpAttachment;
import com.yf1706.bean.EmpWorkHistory;
import com.yf1706.service.EmpService;
import com.yf1706.tools.UploadDownloadTool;
import com.yf1706.tools.UserTools;

@Controller
@RequestMapping("empAttachment")
public class EmpAttachmentController {
	@Autowired
	private UserTools ut;
	@Autowired
	private EmpService es;
	@Autowired
	private UploadDownloadTool udt;

	// 根据员工编号新增附件记录
	@RequestMapping("addEmpAttachment")
	public void addEmpAttachment(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		Map map = new HashMap<>();
		try {
			out = response.getWriter();
			System.out.println("进入addEmpAttachment");

			int empId = Integer.valueOf(request.getParameter("empId"));
			String attachmentType = request.getParameter("attachmentType");
			String remark = request.getParameter("remark");
			
			//将文件上传，并返回文件路径
			String attachmentAddress=udt.fileUpload(file, request);
			
			EmpAttachment ea=new EmpAttachment(attachmentAddress, attachmentType, remark);
			es.addEmpAttachmentByEmpId(empId, ea);
			System.out.println(ea);
			map.put("success", true);
			map.put("msg", "添加成功");
			String msg = JSON.toJSONString(map);
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	// 根据编号删除附件记录
	@RequestMapping("deleteEmpAttachmentByAttachmentId")
	public void deleteEmpAttachmentByAttachmentId(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		Map map = new HashMap<>();
		try {
			System.out.println("进入deleteEmpAttachmentByAttachmentId");
			System.out.println("id_arr" + request.getParameter("id_arr"));
			JSONArray arr = JSONArray.parseArray(request.getParameter("id_arr"));
			for (Object obj : arr) {
				int attachmentId = (int) obj;
				es.deleteEmpAttachmentByAttachmentId(attachmentId);
			}
			map.put("success", true);
			map.put("msg", "删除成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 修改对应编号的附件记录
	@RequestMapping("updateEmpAttachmentByAttachmentId")
	public void updateEmpAttachmentByAttachmentId(HttpServletRequest request, HttpServletResponse response) {
		Map map = new HashMap<>();
		PrintWriter out;
		try {

			System.out.println("进入updateEmpAttachmentByAttachmentId");

			int attachmentId = Integer.valueOf(request.getParameter("attachmentId"));
			String attachmentType = request.getParameter("attachmentType");
			String remark = request.getParameter("remark");

			es.updateEmpAttachmentByAttachmentId(attachmentId, attachmentType,remark);

			map.put("success", true);
			map.put("msg", "编辑成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
